package ua.zwpp.java8.streams;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by vitaly on 17.09.14.
 */
public class Demo {

    public static void main(String[] args) {
        List<String> langs = Arrays.asList("C#", "Java", "Scala", "Groovy", "Clojure", "Lisp", "Haskell");

        langs.stream().
                map(String::toUpperCase).
                sorted().
                filter(l -> l.contains("A")).
                forEach(System.out::println);

        double average = langs.stream().mapToInt(String::length).average().getAsDouble();
        System.out.println("langs length average = " + average);

        long containsACount = langs.stream().filter(l -> l.contains("a")).count();
        System.out.println("containsACount = " + containsACount);

        Optional<String> reduced = langs.stream().reduce((a, b) -> a + ", " + b);
        reduced.ifPresent(System.out::println);

        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> evenNumbersList = stream.filter(i -> i % 2 == 0).collect(Collectors.toList());
        System.out.println("even numbers = " + evenNumbersList);

        Stream<Integer> arrStream = Arrays.stream(new Integer[] {3, 5, 7, 3, 5, 3, 8, 4});
        System.out.println("max = " + arrStream.reduce(0, Integer::max).intValue());

        IntStream.rangeClosed(3, 7).forEach(System.out::println);
    }
}
